<?php
	$LOCALPATH = realpath(dirname(__FILE__)); 
	require_once( $LOCALPATH . "./include/classes.php");
	session_start();

	$uid = $_POST['uid'] ?? '' ; 
	$passwd = $_POST['passwd'] ?? '' ; 
	$message = '';
	if($uid && $passwd){

		$dao = new AuthDao();
		try{
			$authorized = $dao->auth( $uid , $passwd );
			if( $authorized )
			{
				$_SESSION[_AUTH] = $uid;

				header("Location: /meter/users.php");
				exit();
			} else {
				$message = 'User Or Password Error !' ;
			}

		}catch(Exception $e){
			$message = $e->getMessage();
		}		
	}


?>

<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">

<title>登录</title>

    <?php
        require_once("pages/resources.php");
    ?>
 
    <script src="/meter/resources/jquery/jquery-2.0.3.js"></script>
    
    <script src="/meter/resources/js-base-util.js"></script>
    <script type="text/javascript" src="/meter/resources/layer/layer.js"></script>
  	<script src="/meter/resources/js/jquery.cookie.js"></script>
	<script src="/meter/resources//js/sha1.js"></script>

</head>
<body  id="deviceCtrl" >

  
    <!-- Page container -->
    <div class="page-container">

                 
        <center>
        	<div style="margin-top: 200px">
        <!-- Form begin -->
        <form id="loginform" method="POST" style="width: 40%; top: 200px" class="form-horizontal" role="form"   >
            <div class="panel panel-default">
                
                <div class="panel-body">
               
                    <div class="form-group  ">
                        <label class="col-sm-4 control-label"> User  :</label>
                        <div class="col-sm-8">
                            <input type="text" name="uid" class="form-control" id="_INPUT_USERID" value="<?=$uid?>"  
                               
                             >  
                        </div>
                    </div>
                    <div class="form-group ">
                        <label class="col-sm-4 control-label">  Password :</label>
                            <div class="col-sm-8">
                                <input type="password" name="passwd"  class="form-control" id="_INPUT_PASS" value="" >  
                            </div>
                    </div>
                  	
                  	<div style="color: red; float: left" id="loginErrorMsg">
                  		<?=$message?>
                  	</div>
                 

                    <div class="form-actions text-right">
                        <input type="button" onclick="user_login()" value="Login"   class="btn btn-primary">
                    </div>

                </div>
            </div>
        </form>
        	</div>
    	</center>

</body>
 
<script type="text/javascript">
	$(document).ready(function() {
		var uid = $.cookie("_login_account");
		if(!uid)
		{
			$("#_INPUT_USERID").focus();
			return;
		}else{
			$("#_INPUT_USERID").val(uid);		
			$("#_INPUT_PASS").focus();
		}
	});
	
	function user_login(){

        var uid = $('#_INPUT_USERID').val();
        var pass = $('#_INPUT_PASS').val();


		if( !uid ){
			$("#loginErrorMsg").empty();
			$("#loginErrorMsg").append("<span>账号不能为空,请重新输入！</span>");         // 追加新元素
			$("#username").focus();
			return false;
		}
		if(!pass)
		{
			$("#loginErrorMsg").empty();
			$("#loginErrorMsg").append("<span>密码不能为空,请重新输入！</span>");
			$("#password").focus();
			return false;
		}

	 	$.cookie('_login_account', uid , {expires: 7});
	 	$('#loginform').submit();
		 
        return true;
			
			 
	 
	}

	function setCookie() {
		if($('#inlineCheckbox1')[0].checked) {//default expiration time: 7 days
			$.cookie('btmpUid',$("#username").val(), {expires: 7});
		}
	}
	
	function keydowunLogin(obj){
		var event=arguments.callee.caller.arguments[0]||window.event;//消除浏览器差异  
		if(event.keyCode==13){
			user_login();
		}
	}

	</script>
	</body>

</html>